package com.dingding.service.activity.core.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.ActivityRelUser;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.activity.core.dao.IActivityRelUserDao;
import com.dingding.service.activity.core.sql.ActivityRelUserSQL;

/**
 * 
 * @ClassName: ActivityRelUserDaoImpl
 * @Description: 活动和用户关联关系操作DAO
 * @author CHEHAUNBO
 * @date 2016年4月1日 下午2:23:56
 * 
 */
@Component("activityRelUserDao")
public class ActivityRelUserDaoImpl extends BaseDaoImpl<ActivityRelUser> implements IActivityRelUserDao {

	private static final Logger log = Logger.getLogger(ActivityRelUserDaoImpl.class);

	/**
	 * 
	 * @Title: getActivityRelUser
	 * @Description:获取活动和用户的关联关系
	 * @param userId
	 *            用户ID
	 * @param activityId
	 *            活动ID
	 * @param type
	 *            查询类型 1:优惠券 2：积分赠送
	 *            由于活动和用户关联表同时承担积分赠送活动和优惠券赠送的的关联任务，所以需要根据查询类型区别具体关联哪张表
	 * @param isAdd
	 * 			  如果不存在用户是否添加
	 * @return ActivityRelUser
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ActivityRelUser getActivityRelUser(String userId, String activityId, int type, boolean isAdd) {

		List<ActivityRelUser> activityRelUsers = null;
		try {
			activityRelUsers = super.listHql(ActivityRelUserSQL.GET_ACTIVITYTRELUSER_BY_USERID, userId, activityId,
					type);
		} catch (Exception e) {
			log.error("获取活动和用户的关联关系出现异常！", e);
		}

		ActivityRelUser activityRelUser = null;
		if (activityRelUsers != null && activityRelUsers.size() > 0) {
			activityRelUser = activityRelUsers.get(0);
		} else if (isAdd) { // 不存在当前关联关系，进行添加
			activityRelUser = addActivityRelUser(userId, activityId, type);
		}

		return activityRelUser;
	}

	/**
	 * 
	 * @Title: addActivityRelUser
	 * @Description: 添加用户和活动关联关系
	 * @param userId
	 *            用户ID
	 * @param activityId
	 *            活动ID
	 * @param type
	 *            类型（1：关联积分赠送活动 2：关联优惠券赠送活动）
	 * 
	 * @return ActivityRelUser 添加成功后的用户和活动关联信息
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ActivityRelUser addActivityRelUser(String userId, String activityId, int type) {

		ActivityRelUser activityRelUser = new ActivityRelUser();
		activityRelUser.setActivityRelUserId(ToolUtil.getUid()); // 主键ID
		activityRelUser.setLoginId(userId);// 用户ID
		activityRelUser.setActivityId(activityId); // 活动ID
		activityRelUser.setActivityType(type);// 活动类型
		activityRelUser.setDrawNumber(0); // 默认未领取过
		try {
			super.save(activityRelUser);
		} catch (Exception e) {
			log.error("添加用户和活动关联关系表出现异常！", e);
			return null;
		}

		return activityRelUser;
	}

	/**
	 * 
	 * @Title: addAttendActivityNumber 
	 * @Description: 修改用户参加活动次数
	 * @param userId 用户ID 
	 * @param activityId 活动ID 
	 * @param type 活动类型 1：关联积分活动 2:关联优惠券活动
	 * @return boolean  true 成功 false 失败 
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addAttendActivityNumber(String userId, String activityId, int type) {

		try {
			int row = super.updateSqlRow(ActivityRelUserSQL.ADD_USERATTENDACTIVITY_NUMBER, activityId, type, userId);

			if (row > 0) { // 添加成功
				return true;
			}
		} catch (Exception e) {
			log.error("添加用户参加活动次数出现异常！", e);
		}

		return false;
	}

}
